<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MVersionCheckController
extends MApplicationComponent
implements MIController
{
	
    public function invoke() {
        parent::init();
                                $version = NULL;
        $client  = NULL;
        $name    = NULL;
        
        if (isset($_REQUEST["version"])) {
            $version = $_REQUEST["version"];
        }
        
        if (isset($_REQUEST["client"])) {
            $client = $_REQUEST["client"];
        }
        
        if (isset($_REQUEST["name"])) {
            $name = $_REQUEST["name"];
        }
                                if (is_null($version) || is_null($client) || is_null($name)) {
            throw new MException(FXP::t(MConst::PARAMS_ERROR), MConst::HTTP_CODE_400);
        }
        
                                $response            = array();
        $response["update"]  = false;
        $response["version"] = $version;
                                $upgrade = MSystemUpgrade::queryUpgradeByName($name, $client);
        if (count($upgrade) != 0) {
                                                $ver         = str_replace(".", "", $version);
            $max_upgrade = $upgrade[count($upgrade) - 1];
            $max_ver     = $max_upgrade["version"];
            $max_ver     = str_replace(".", "", $max_ver);
                                                
            if ((int)$version < (int)$max_ver) {
                $response["update"]      = true;
                $response["version"]     = $max_upgrade["version"];
                $response["size"]        = $max_upgrade["size"];
                $response["signature"]   = $max_upgrade["signature"];
                $response["files_count"] = $max_upgrade["files_count"];
                $response["path"]        = $max_upgrade["path"];
                $response["logs"]        = $max_upgrade["logs"];
            }
        }
        echo json_encode($response);
    }
}
?>